<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 分类模型
 * @author  Karson
 * @name    category.php
 * @since   2012-12-13 23:06:46
 */
class Category_Model extends Model {

    var $systemcategory = array();
    var $usercategory = array();
    var $allcategory = array();

    function _init() {
        //读取系统分类缓存
        $systemcategory = $this->cache->get("systemcategory");
        if (!$systemcategory) {
            $systemcategory = array();
            $this->db->query("SELECT * FROM {pre}category WHERE user_id='0' AND state='1' ORDER BY weigh ASC");
            while ($row = $this->db->get_array()) {
                $systemcategory[$row['id']] = $row;
            }
            $this->cache->set("systemcategory", $systemcategory);
        }
        $this->systemcategory = $systemcategory;
    }

    //读取会员相关的分类
    function load_user_category($user_id = null) {
        $user_id = !is_null($user_id) ? $user_id : $this->auth->user_id;
        $this->db->query("SELECT * FROM {pre}category WHERE user_id='{$user_id}' ORDER BY weigh ASC");
        $usercategory = array();
        while ($row = $this->db->get_array()) {
            $usercategory[$row['id']] = $row;
        }
        $this->usercategory = $usercategory;
        return $usercategory;
    }

    //加载家庭分类信息
    function load_family_category($family_id) {
        $this->db->query("SELECT * FROM {pre}category WHERE user_id IN (SELECT id FROM {pre}user WHERE family_id='{$family_id}') AND state='1' ORDER BY weigh ASC");
        $usercategory = array();
        while ($row = $this->db->get_array()) {
            $usercategory[$row['id']] = $row;
        }
        $this->usercategory = $usercategory;
        return $usercategory;
    }

    function get_category($id) {
        if (!$this->allcategory) {
            $this->allcategory = $this->usercategory ? $this->usercategory : $this->load_user_category();
            $this->allcategory = $this->allcategory + $this->systemcategory;
        }
        if (isset($this->allcategory[$id])) {
            return $this->allcategory[$id];
        } else {
            return array('id' => 0, 'name' => '未知');
        }
    }

    function get_category_list($type, $father_id = null, $user_id = null) {
        $user_id = !is_null($user_id) ? $user_id : $this->auth->user_id;
        if ($user_id > 0) {
            $categorylist = $this->usercategory ? $this->usercategory : $this->load_user_category($user_id);
        } else {
            $categorylist = $this->systemcategory;
        }
        $result = array();
        foreach ($categorylist as $m => $n) {
            if ($user_id > 0 && $user_id != $n['user_id'])
                continue;
            if ($n['type'] == $type) {
                if (!is_null($father_id)) {
                    if ($n['father_id'] == $father_id) {
                        $result[] = $n;
                    }
                } else {
                    $result[] = $n;
                }
            }
        }
        return $result;
    }

    function get_category_option($type, $father_id = null, $selected = null, $first = null, $user_id = null) {
        $selected = is_array($selected) ? $selected : is_null($selected) ? array() : array($selected);
        $list = $this->get_category_list($type, $father_id, $user_id);
        if ($first && is_array($first)) {
            $list = $first + $list;
        }
        foreach ($list as $k => $v) {
            if ($user_id > 0 && $user_id != $v['user_id'])
                continue;
            $text = in_array($v['id'], $selected) ? " selected" : '';
            $result[] = "<option value='{$v['id']}'{$text}>{$v['name']}</option>";
        }
        return implode('', $result);
    }

}